﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using AspNetCs.Model;
using AspNetCs.DALFactory;
using AspNetCs.SQLServerDAL;
using AspNetCs.IDAL;
using AspNetCs.Config;
using AspNetCs.Common;
using System.Web;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Enumerations.TrueOrFalseEnum;

namespace AspNetCs.Bll
{
    public class FindFamilyBLL
    {
        IFindFamily dal = Conn.CreateFindFamily();

        /// <summary>
        /// 添加寻亲信息
        /// </summary>
        public int AddFamilyInfo(FindFamilyModel model)
        {

            return dal.AddFamilyInfo(model);
        }

        /// <summary>
        /// 删除寻亲信息(逻辑删除)
        /// </summary>
        /// <param name="id"></param>
        public int HiddenFamilyInfo(int id)
        {
            return dal.HiddenFamilyInfo(id);
        }

        /// <summary>
        /// 删除寻亲信息(物理删除)
        /// </summary>
        /// <param name="id"></param>
        public int DeleteFamilyInfo(int id)
        {
            return dal.DeleteFamilyInfo(id);
        }

        /// <summary>
        /// 查询用户添加的寻亲信息
        /// </summary>
        /// <param name="CreateId">创建者Id</param>
        /// <param name="CheckedOrNo">查看未审核通过(0)、审核通过(1)、所有的(2)</param>
        /// <param name="QueryPropertys">需要查询的字段如 Id,Name,如需查询所有的字段输入</param>
        /// <returns></returns>
        public DataTable SearchUserCreateFamilyInfo(string QueryPropertys, int CreateId, int CheckedOrNo)
        {
            return dal.SearchUserCreateFamilyInfo(QueryPropertys, CreateId, CheckedOrNo);
        }

        /// <summary>
        /// 修改寻亲信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateFamilyInfo(FindFamilyModel model)
        {
            return dal.UpdateFamilyInfo(model);
        }

        /// <summary>
        /// 通过ID查询寻亲信息
        /// <param name="QueryPropertys">需要查询的字段如 Id,Name,如需查询所有的字段输入</param> 
        /// <param name="CheckedOrNo">编号</param>
        /// <returns></returns>
        public DataTable SearchFamilyInfoById(string QueryPropertys, int Id)
        {
            return dal.SearchFamilyInfoById(QueryPropertys, Id); ;
        }

        /// <summary>
        /// 设置推荐或者取消推荐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示设置推荐 0表示取消推荐</param>
        /// <returns></returns>
        public int SetIntro(int Id, int CancleOrIntro)
        {
            return dal.SetIntro(Id, CancleOrIntro);
        }

        /// <summary>
        /// 审核或者取消审核
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CancleOrIntro">1表示审核 0表示取消审核</param>
        /// <returns></returns>
        public int SetCheck(int Id, int CancleOrIntro)
        {
            return dal.SetCheck(Id, CancleOrIntro);
        }

        /// <summary>
        /// 设置背景音乐
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="MusicId"></param>
        /// <returns></returns>
        public int SetMusic(int Id, int MusicId)
        {
            return dal.SetMusic(Id, MusicId);

        }

        public void filterUpdate(string Upfilter, string filter)
        {
            dal.filterUpdate(Upfilter, filter);
        }

        public FindFamilyModel GetModel(DataRow dr)
        {
            return dal.GetModel(dr);
        }
        public List<FindFamilyModel> GetList(string filter)
        {
            return dal.GetList(filter);
        }


        /// <summary>
        /// 根据id,生成用模板
        /// </summary>
        /// <param name="NumStr">馆号</param>
        /// <param name="chel"></param>
        /// <param name="systemConfig">系统配制信息</param>
        /// <param name="htmlpath">生成的路径</param>
        /// <returns></returns>
        public bool CurrTempleContent(string NumStr, Channel_Type chel, SiteConfigInfo systemConfig, string htmlpath)
        {
            bool Result = false;
            StringBuilder stb = new StringBuilder();
            FindFamilyModel md = new FindFamilyModel();
            try
            {

                DataTable dt = SearchFamilyInfoById("*", StringHelper.isNumber_int(NumStr)); //(" id=" + NumStr + "");
                dt.TableName = "FindFamily";
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        md = GetModel(dr);
                        break;
                    }
                    if (md.IsCheck == (int)IsTrueEnum.NO)
                    {
                        return false;
                    }
                    if (md.TempleId > 0)
                    {
                        Temple_Model TmpMd = TempleBll.CreateNew.GetModel(md.TempleId);
                        if (TmpMd.Id > 0)
                        {
                            string TempleUrl = systemConfig.InstallDir + StringHelper.ReadConfig("TempleSkin") + chel.ToString() + "/" + TmpMd.TemplePath + "/index.html";
                            string Templepath = HttpContext.Current.Server.MapPath(TempleUrl);//模板的真实路径

                            StringBuilder newstr = new StringBuilder();
                            newstr.AppendLine("<script>");
                            newstr.AppendLine("<!--");
                            newstr.AppendLine("var id='" + md.Id + "';");//ID
                            newstr.AppendLine("var UserID='" + md.CreateId + "';");//陵园名称
                            newstr.AppendLine("-->");
                            newstr.AppendLine("</script>");
                            newstr.AppendLine("<script src='" + systemConfig.InstallDir + "Script/jquery.js'></script>");

                            stb = TempleBll.CreateNew.WriteHtml(Templepath, newstr.ToString());//读取模板内容

                            GetStringData_lable lbReplace = new GetStringData_lable();

                            #region 循环所有属性
                            //newstr.AppendLine("<script>");
                            //newstr.AppendLine("<!--");

                            //PropertyInfo[] props = typeof(Cemetery_Model).GetProperties();
                            //foreach (PropertyInfo p in props)
                            //{
                            //    newstr.AppendLine("var " + p.Name.ToString() + "='" + p.GetValue(md, null) + "';");
                            //}
                            //newstr.AppendLine("-->");
                            //newstr.AppendLine("</script>");


                            #endregion

                            string ContentValue = lbReplace.GetResultStr(stb, dt);
                            Result = TempleBll.CreateNew.CreateHtmlFile(ContentValue, htmlpath);
                        }
                    }
                }
            }
            catch
            {
                Result = false;
            }

            return Result;
        }




    }
}
